using System;
using System.Windows.Forms;

using Microsoft.Web.Administration;

namespace Microsoft.Web.Management.Iis.Client
{
    /// <summary>
    /// Allows customization of custom logging flags.
    /// </summary>
    public partial class LogFileFlagsDialog : Form
    {
        /********************************************************************************************
           FIELDS
         *******************************************************************************************/
        Boolean         _initializing = true;

        /********************************************************************************************
           METHODS
         *******************************************************************************************/
        /// <summary>
        /// Creates an instance of a LogFileFlagsDialog object.
        /// </summary>
        public LogFileFlagsDialog()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Executes loading events for the LogFileFlagsDialog form.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void LogFileFlags_Load(object sender, EventArgs e)
        {
            // TODO: Localization
            PopulateTree();
            switch (LoggingMode)
            {
                case LoggingMode.Site:
                    Text = IsServerContext ? 
                        Resources.LogFileFlagDialogLogExtensionSiteDefaults : 
                        Resources.LogFileFlagDialogLogExtensionSite;
                    break;
                case LoggingMode.W3C :
                    Text = Resources.LogFileFlagDialogLogExtensionW3C;
                    break;
                case LoggingMode.Binary:
                    Text = Resources.LogFileFlagDialogLogExtensionBinary;
                    break;
            }
        }

        /// <summary>
        /// Populates the tree with all available log file flags.
        /// </summary>
        private void PopulateTree()
        {
            _initializing = true;
            string[] names = Enum.GetNames(typeof(LogExtFileFlags));
            var rootNode = logFileFlagsTreeView.Nodes[0];
            foreach (string name in names)
            {
                if (name != null)
                {
                    var node = rootNode.Nodes.Add(name);
                    var flag = (LogExtFileFlags)Enum.Parse(typeof(LogExtFileFlags), name);
                    node.Tag = flag;
                    node.Checked = LogExtFileFlags == (LogExtFileFlags | flag);
                }
            }
            rootNode.Expand();
            _initializing = false;
        }

        /// <summary>
        /// Handles node checking and flag modification.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnLogFileFlagsTreeViewAfterCheck(object sender, TreeViewEventArgs e)
        {
            if (!_initializing)
            {
                if (e.Node.Parent != null)
                {
                    if (e.Node.Checked)
                    {
                        LogExtFileFlags |= (LogExtFileFlags)e.Node.Tag;
                    }
                    else
                    {
                        LogExtFileFlags &= ~(LogExtFileFlags)e.Node.Tag;
                    }
                }
                else
                {
                    foreach (TreeNode tn in e.Node.Nodes)
                    {
// ReSharper disable RedundantCheckBeforeAssignment
                        if (tn.Checked != e.Node.Checked)
// ReSharper restore RedundantCheckBeforeAssignment
                        {
                            tn.Checked = e.Node.Checked;
                        }
                    }
                }
            }
        }

        /********************************************************************************************
           PROPERTIES
         *******************************************************************************************/

        /// <summary>
        /// Gets or sets the logging extension file flags.
        /// </summary>
        internal LogExtFileFlags LogExtFileFlags { get; set; }

        /// <summary>
        /// Gets or sets a value indicating whether the call is in the server context.
        /// </summary>
        internal bool IsServerContext { get; set; }

        /// <summary>
        /// Gets or sets the logging mode for the current page.
        /// </summary>
        internal LoggingMode LoggingMode { get; set; }
    }
}